<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc'
	);
	$list=$this->get_list('area_agent_area',$parameter,$total);
	if($list){
		$open_duodai=$this->setting['open_duodai'];
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			if($item['is_duodai']){
				$item['address']=$item['duodai'];
			}else{
				$item['address']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
			}
			$item['open_duodai']=$this->setting['open_duodai'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'address','title'=>'代理地区','type'=>'string'),
			array('name'=>'level_id','title'=>'区域代理等级','type'=>'select','options'=>'levels'),
			array('name'=>'status','title'=>'区域代理状态','type'=>'radio','options'=>'apply_status'),
			array('name'=>'is_duodai','title'=>'多省代？','type'=>'radio','options'=>'is_duodai','show'=>array('open_duodai'=>1)),
			array('name'=>'order_num','title'=>'区域订单数','type'=>'string'),
			array('name'=>'order_price','title'=>'区域订单总金额','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'setlevel','title'=>'设置等级'),
			array('type'=>'href','op'=>'duodai','title'=>'设置代理模式'),
			array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>0)),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'状态','options'=>'apply_status'),
			array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['apply_status']=$this->apply_status;
	$return['is_duodai']=array('不是','是');
	$return['levels']=$this->getall('area_level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$apply_id=intval($post['id']);
		$status=intval($post['status']);
		if(!$status){
			exi('请选择审核通过还是不通过！','error');
		}
		$this->model('area/member')->check_agent($apply_id,$status,$post['remark']);
		exi('审核成功！','','list');
	}
	$item=$this->get('area_agent_area',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定区域代理不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'审核区域代理申请',
		'edit'=>array(
			array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'apply_status'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'text','show'=>array('status'=>2)),
		),
	);
	$return['apply_status']=$this->apply_status;
	exi($return);
}
if($_RQ['op']=='duodai'){
	//选择代理地区
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['is_duodai'] && !$post['area']){
			exi('请先选择代理地区~','error');
		}
		if($post['is_duodai'] && !$post['duodai']){
			exi('请先选择省~','error');
		}
		if(!$post['is_duodai'] && $post['area']){
			$area_code=$this->model()->get_area_code($post['area']);
		}
		$agent_id=$this->model('area/member')->up_agent($post['mid'],$area_code,$post);
		$this->model('member')->check_agent($agent_id,1);
		exi('设置成功！','','list');
	}
	$item=$this->get('area_agent_area',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定区域代理不存在！','error');
	}
	$item['area']=$item['area']?json_decode($item['area'],true):array('city'=>'');
	$return['item']=$item;
	$areas=cfc('tools')->pcd();
	$return['areas']=array();
	foreach($areas as $k=>$v){
		$return['areas'][$k]=$k;
	}
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置省',
		'edit'=>array(
			array('name'=>'is_duodai','title'=>'多省代？','type'=>'radio','options'=>'is_duodai'),
			array('type'=>'checkbox','name'=>'duodai','title'=>'选择省','options'=>'areas','show'=>array('is_duodai'=>1)),
			array('type'=>'pcd','name'=>'area','title'=>'选择代理地区','show'=>array('is_duodai'=>0)),
		),
	);
	$return['is_duodai']=array('不是','是');
	exi($return);
}
if($_RQ['op']=='setlevel'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$this->update('area_agent_area',array('level_id'=>intval($post['level_id'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！','','list');
	}
	$item=$this->get('area_agent_area',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定区域代理不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置代理商等级',
		'edit'=>array(
			array('name'=>'level_id','title'=>'区域代理等级','type'=>'select','options'=>'levels'),
		),
	);
	$return['levels']=$this->getall('area_level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		$mid=$this->getcolumn('area_agent_area',array('id'=>$id),'mid');
		$this->delete('area_agent_area',array('id'=>$id));
		$this->delete('area_member',array('mid'=>$mid));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$mid=$this->getcolumn('area_agent_area',array('id'=>$id),'mid');
				$this->delete('area_agent_area',array('id'=>$id));
				$this->delete('area_member',array('mid'=>$mid));
			}
		}
	}
	exi('删除成功！');
}
?>